#include <bits/stdc++.h>
using namespace std;

int t, n, sz, ans, ind[200005], pre[200005];

#define lowbit(x) (x & -x)

bool check(int t1, int t2, int t3)
{
    // cout << t1 << " " << t2 << " " << t3 << endl;
    int diff1, diff2, diff3;
    int pos = lower_bound(pre + 1, pre + sz + 1, t1) - pre;
    if (pre[pos] > t1)
        pos--;
    diff1 = t1 - pre[pos];
    int pos2 = lower_bound(pre + 1, pre + sz + 1, t1 + t2 - diff1) - pre;
    if (pre[pos2] > t1 + t2 - diff1)
        pos2--;
    diff2 = t1 + t2 - pre[pos2];
    int pos3 = lower_bound(pre + 1, pre + sz + 1, t1 + t2 + t3 - diff2) - pre;
    if (pre[pos3] > t1 + t2 + t3 - diff2)
        pos3--;
    return pos3 == sz;
}

int main()
{
    cin >> t;
    while (t--) {
        cin >> n;
        memset(pre, 0, sizeof pre);
        sz = 0;
        ans = 0x3f3f3f3f;
        map<int, int> mp;
        for (int a, i = 1; i <= n; i++)
            cin >> a, mp[a]++;
        for (auto x : mp)
            ind[++sz] = x.second, pre[sz] = pre[sz - 1] + ind[sz];
        pre[sz + 1] = 0x3f3f3f3f;
        for (int i = 0; i <= 20; i++) {
            for (int j = 0; j <= 20; j++) {
                for (int k = 0; k <= 20; k++) {
                    if ((1 << i) + (1 << j) + (1 << k) < n)
                        continue;
                    if (check(1 << i, 1 << j, 1 << k))
                        ans = min(ans, (1 << i) + (1 << j) + (1 << k));
                }
            }
            if ((1 << i) > ans)
                break;
        }
        cout << ans - n << endl;
    }
}
